﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;
using Facade;
using System.Collections;
using System.Data;

namespace IronOne.SalesOne.SalesOneWeb.ContactManagement
{
    public partial class FinanceDetails : SOSecurityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    ValidateUserCredentials(Request.Url.LocalPath);
                    ViewState["refUrl"] = Request.UrlReferrer.ToString();
                }

                if (!string.IsNullOrEmpty(Request.QueryString["nid"]))
                {
                    int need = Convert.ToInt32(Request.QueryString["nid"]);

                    if (Session["na"] != null)
                    {
                        SalesOne.BusinessObjects.NeedsAnalysis na = (SalesOne.BusinessObjects.NeedsAnalysis)Session["na"];

                        //get income
                        lblIncome.Text = String.Format("{0:0,0.00}", na.Inc);
                        lblOtherIncome.Text = String.Format("{0:0,0.00}", na.OtherIncome);
                        lblSpouseIncome.Text = String.Format("{0:0,0.00}", na.SpouseIncome);
                        lblSavingInv.Text = String.Format("{0:0,0.00}", na.SavingInc);
                        lblTotalInc.Text = String.Format("{0:0,0.00}", na.Income);


                        //get liability
                        lblUnprLoans.Text = String.Format("{0:0,0.00}", na.UnpLoans);
                        lblTotalLia.Text = String.Format("{0:0,0.00}", na.Liability);
                        lblOtherLoans.Text = String.Format("{0:0,0.00}", na.Liability - na.UnpLoans);

                        //get expenses
                        lblFood.Text = String.Format("{0:0,0.00}", na.Food);
                        lblTrans.Text = String.Format("{0:0,0.00}", na.Trans);
                        lblOthers.Text = String.Format("{0:0,0.00}", na.OtherExp);
                        lblCredit.Text = String.Format("{0:0,0.00}", na.CredPay);
                        lblHouseExp.Text = String.Format("{0:0,0.00}", na.HouseExp);
                        lblSchFees.Text = String.Format("{0:0,0.00}", na.SchFees);
                        lblLoanIns.Text = String.Format("{0:0,0.00}", na.LoanIns);
                        lblTotalExp.Text = String.Format("{0:0,0.00}", na.Expenses); 

                        //get assets
                        lblDep.Text = String.Format("{0:0,0.00}", na.FixedDep);
                        lblOthCey.Text = String.Format("{0:0,0.00}", na.OthCey);
                        lblOthAss.Text = String.Format("{0:0,0.00}", na.OthAss);
                        lblSavings.Text = String.Format("{0:0,0.00}", na.Saving);
                        lblOthIns.Text = String.Format("{0:0,0.00}", na.OthIns);
                        lblTotalAssests.Text = String.Format("{0:0,0.00}", na.Assets);


                    }
                    
                    //CustomerFacade cFac = new CustomerFacade();
                    //SalesOne.BusinessObjects.NeedsAnalysis na = cFac.GetNeedAnalysis(need);

                   


                }
                else
                {
                    FinanceFacade fFac = new FinanceFacade();
                    if (Session["cusId"] != null)
                    {
                        //get income
                        string custId = Session["cusId"].ToString();
                        Income inc = fFac.GetIncomeForCustomer(custId);

                        if (inc != null)
                        {
                            lblIncome.Text = String.Format("{0:0,0.00}", inc.IncomeValue);
                            lblOtherIncome.Text = String.Format("{0:0,0.00}", inc.OtherIncome);
                            lblSpouseIncome.Text = String.Format("{0:0,0.00}", inc.SpousesIncome);
                            lblSavingInv.Text = String.Format("{0:0,0.00}", inc.Savings);
                            lblTotalInc.Text = String.Format("{0:0,0.00}", (inc.IncomeValue + inc.OtherIncome + inc.SpousesIncome + inc.Savings));
                        }
                        else
                        {
                            lblIncome.Text = "0.00";
                            lblOtherIncome.Text = "0.00";
                            lblSpouseIncome.Text = "0.00";
                            lblSavingInv.Text = "0.00";
                            lblTotalInc.Text = "0.00";

                        }



                        //get liability

                        Liability l = fFac.GetLiabilitiesForCustomer(custId);
                        lblUnprLoans.Text = String.Format("{0:0,0.00}", l.UnProLoans);
                        lblOtherLoans.Text = String.Format("{0:0,0.00}", l.Others);
                        lblTotalLia.Text = String.Format("{0:0,0.00}", (l.UnProLoans + l.Others)); 


                        //get expenses
                        Expense ex = fFac.GetExpensesForCustomer(custId);
                        lblFood.Text = String.Format("{0:0,0.00}", ex.Food);
                        lblTrans.Text = String.Format("{0:0,0.00}", ex.Transport);
                        lblOthers.Text = String.Format("{0:0,0.00}", ex.OtherExp);
                        lblCredit.Text = String.Format("{0:0,0.00}", ex.CreditPay);
                        lblHouseExp.Text = String.Format("{0:0,0.00}", ex.HouseExp); 
                        lblSchFees.Text = String.Format("{0:0,0.00}", ex.SchFees);
                        lblLoanIns.Text = String.Format("{0:0,0.00}", ex.LoanInst);
                        lblTotalExp.Text = String.Format("{0:0,0.00}", (ex.Food + ex.Transport + ex.OtherExp + ex.CreditPay + ex.HouseExp + ex.SchFees + ex.LoanInst));


                        //Assets
                        Asset ass = fFac.GetAssetsForCustomer(custId);
                        lblDep.Text = String.Format("{0:0,0.00}", ass.FixedDepo);
                        lblOthCey.Text = String.Format("{0:0,0.00}", ass.OthCeyIns);
                        lblOthAss.Text = String.Format("{0:0,0.00}", ass.Others);
                        lblSavings.Text = String.Format("{0:0,0.00}", ass.Saving);
                        lblOthIns.Text = String.Format("{0:0,0.00}", ass.OthIns);
                        lblTotalAssests.Text = String.Format("{0:0,0.00}", (ass.FixedDepo + ass.OthCeyIns + ass.Others + ass.Saving + ass.OthIns));

                    }
                }
            }
            catch (Exception ex)
            {               
                //lblError.Text = ex.Message;
            }
            

                

        }

        protected void TextBox6_TextChanged(object sender, EventArgs e)
        {

        }

        protected void TextBox12_TextChanged(object sender, EventArgs e)
        {

        }

        protected void cancelButton_Click(object sender, EventArgs e)
        {
            Response.Redirect(ViewState["refUrl"].ToString());
        }
    }
}
